package com.lanou3g.mynewstest.module.news.jokes;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseFragment;
import com.lanou3g.mynewstest.beans.NewsJokesBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.lanou3g.mynewstest.tools.RefreshableListView;
import com.lanou3g.mynewstest.tools.StringRequestUtf;

import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class JokesFragment extends MyBaseFragment {
    private final String url = "http://c.3g.163.com/recommend/getChanListNews?channel=duanzi&size=20";
    private ListView listView;
    private JokesAdapter adapter;
    private NewsJokesBeans beans;
    private RefreshableListView refreshableListView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.from(context).inflate(R.layout.fragment_news_jokes, container, false);
        listView = (ListView) view.findViewById(R.id.jokes_lv);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            boolean isShowTop = false;
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem+visibleItemCount == totalItemCount) {
                    if (!isShowTop) {
                        getSecond();
                    }
                    isShowTop = true;

                } else {
                    isShowTop = false;
                }
            }
        });
        getBeans();
        refreshableListView = (RefreshableListView) view.findViewById(R.id.jokes_rf);
        refreshableListView.setOnRefreshListener(new RefreshableListView.PullToRefreshListener() {
            @Override
            public void onRefresh() {
                getBeans();
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                refreshableListView.finishRefreshing();
            }
        }, 0);

        return view;
    }

    private void getBeans() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                beans = gson.fromJson(response, NewsJokesBeans.class);
                adapter = new JokesAdapter(context, beans);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        MyRequestQueue.addRequest(request);
    }


    private void getSecond() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                NewsJokesBeans secondBean = gson.fromJson(response, NewsJokesBeans.class);
                List<NewsJokesBeans.JokesEntity> data = secondBean.getJokes();
                if (null!=beans) {
                    beans.getJokes().addAll(data);
                    adapter.addData(beans);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        MyRequestQueue.addRequest(request);
    }


}
